package com.dandandog.framework.faces.utils;

import com.dandandog.framework.faces.aspect.NoticeTarget;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 * @Author: JohnnyLiu
 * @Date: 2021/11/17 16:25
 */
public class FacesMessageUtils {

    public static void show(String clientId, FacesMessage facesMessage) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(clientId, facesMessage);
        if (facesMessage.getSeverity().equals(FacesMessage.SEVERITY_ERROR)) {
            context.validationFailed();
        }
    }

    public static void show(String clientId, FacesMessage.Severity severity, String summary, String detail) {
        FacesMessage facesMessage = new FacesMessage(severity, summary, detail);
        show(clientId, facesMessage);
    }


    public static void show(NoticeTarget target) {
        show(target.getClientId(), target.getSeverity(), target.getSummary(), target.getDetail());
    }

}
